<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Address;
use App\Qingwuit\Models\InviteCode;
use App\Qingwuit\Models\Invites;
use App\Qingwuit\Models\Retails;
use App\Qingwuit\Models\Riders;
use App\Qingwuit\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class InvitesController extends Controller
{
    protected $modelName = 'Invites'; #对应Laravel 生成的 Model文件
    public function index(Request $request)
    {
        $userid = $this->getUserId('users');
//        print_r($userid);die;
        $data = $this->getService('base')
            ->getPageData($this->modelName, ['shop_id' => $userid]);
        return $this->handle($data);
    }
    public function createCode(Request $request)
    {

        $id = $request->id??'';

        $shop_id = $request->shop_id??'';
        $invitati = Invites::where('id',$id)->first();
        if (!$invitati){
            return  $this->error('请传入正确的ID');
        }
        if (is_null($invitati->percentage)){
           return  $this->error('请填写加价百分比');
        }
        $distribution = $request->distribution_id??'';
        $invitationCode = $this->CreateCode1();
        $data = [
            'distribution_id'=>$shop_id,
            'pid'  =>  $id,
            'code'  =>  $invitationCode,
        ];
        $rs = $this->getService('inviteCode', true)->create($data);
        return $this->success( [$invitationCode],__('tip.success'));
    }
    public function CreateCode1() {

        $code = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        $rand = $code[rand(0,61)]
            .strtoupper(dechex(date('m')))
            .date('d').substr(time(),-5)
            .substr(microtime(),2,5)
            .sprintf('%02d',rand(0,99));
        for(
            $a = md5( $rand, true ),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 8;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
            $f++
        );
        return $d;
    }

    //填写邀请码
    public function distribution(Request $request)
    {
        $code = $request->code;
        $shopid = $this->getUserId('users');
        $address_id = Address::where('user_id',$shopid)->first();
//        dd($address_id);
        if (is_null($address_id)){
            return $this->error('请补全信息');
        }
        $invitation = InviteCode::where('code', $code)->first();
//        print_r($invitation);die;
        if (!$invitation){
            return $this->error('邀请码无效');
        }

        $is_yanz = Retails::where('code',$code)->first();
//        dd($is_yanz);
        if ($is_yanz){
            return $this->error('邀请码只能用一次');
        }
        $store = Store::where('user_id',$invitation->distribution_id)->first();
        $data = [
            'shop_id'        => $shopid,
            'distribution_id'=> $store->id,
            'code'=> $code
        ];
            $rs = $this->getService('retails', true)->create($data);
        return $this->success($rs);
    }


//邀请列表
    public function invitesList()
    {
        $user_id = $this->getUserId('users');
//        $user_id = 137;
        $data = Invites::where('shop_id',$user_id)->get();
        return $this->success($data);
    }


    public function updateinvite()
    {
        $id = \request()->id;
        $percentage = \request()->percentage;
        if (is_numeric($percentage)) {
            $data = Invites::where('id',$id)->first();
            $data->percentage = $percentage;
            $data->save();
            return $this->success('修改成功');
        }else {
            return $this->error('请填写数字');
        }


    }



    public function statistics()
    {
        $user_id = $this->getUserId('users');
        if (empty($user_id)) return $this->error('请登录');
        $store_id = Store::where('user_id',$user_id)->first();
//        dd($store_id);
        if (empty($store_id)) return $this->error('没有店铺权限');
        $tyep = \request()->type;
        if (is_null($tyep)){
            return $this->error('缺少参数');
        }
        $yesterday = Carbon::yesterday();
        $today = Carbon::today();
        if ($tyep == 1){
            $order_num = DB::table('orders')->where('store_id',$store_id->id)->whereDate('created_at',$today)->count();
            $order_price = DB::table('orders')->where('store_id',$store_id->id)->whereDate('created_at',$today)->sum('total_price');

        }
        if ($tyep == 2){
            $order_num = DB::table('orders')->where('store_id',$store_id->id)->whereDate('created_at',$yesterday)->count();
           $order_price = DB::table('orders')->where('store_id',$store_id->id)->whereDate('created_at',$yesterday)->sum('total_price');

        }
        if ($tyep == 3){
            $order_num = DB::table('orders')->where('store_id',$store_id->id)->count();
            $order_price = DB::table('orders')->where('store_id',$store_id->id)->sum('total_price');

        }
//        $order_today_num =  DB::table('orders')->where('store_id', $store_id->id)->whereDate('created_at', date('y-m-d'))->count();
//        $order_today_price = DB::table('orders')->where('store_id', $store_id->id)->whereDate('created_at', date('y-m-d'))->sum('total_price');
        $data =   [
            'order_num' => $order_num,
            'order_price' => $order_price,
//            'order_today_num' => $order_today_num,
//            'order_today_price' => $order_today_price,
        ];

        return $this->success($data,'查询成功');
    }
}
